<?php

namespace app\admin\controller;


use app\admin\model\{$model};
use cmf\controller\AdminBaseController;
use think\facade\Db;

{$adminMenu}
class {$controller}Controller extends AdminBaseController
{
    {$menu}
    public function index(){
        //搜索部分
//        $key_status = input('get.key_status/d');

        $lists = {$model}::order('id desc');
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }

        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);

        return $this->fetch('', ['lists' => $lists->toArray(), 'page' => $lists->render(),'domain'=>request()->domain().'/upload/']);
    }

    public function add(){
        {$sqlTpl}
        return $this->fetch('',['domain'=>request()->domain().'/upload/']);
    }

    public function addPost(){
        $params = array_map(function($v){
            return is_array($v)?implode(',',$v):$v;
        },input('post.'));

        if(!$params){
            $this->error('非法参数');
        }

        {$model}::create($params);

        $this->success('操作成功');
    }

    public function edit(){
        $id = input('get.id/d');

        if(!is_numeric($id)){
            $this->error('非法参数');
        }

        $res = {$model}::where('id',$id)->find();
        {$sqlTpl}

        return $this->fetch('',['vo'=>$res,'domain'=>request()->domain().'/upload/']);
    }

    public function editPost(){
        $params = array_map(function($v){
                    return is_array($v)?implode(',',$v):$v;
                    },input('post.'));

        if(!$params || empty($params['id'])){
            $this->error('非法参数');
        }

        {$model}::update($params);

        $this->success('操作成功');
    }

    public function del(){
        $id = input('get.id/d');

        if(!(is_numeric($id) && $id>0)){
            $this->error('非法参数');
        }

        {$model}::where('id',$id)->delete();

        $this->success('删除成功');
    }

    public function field(){
         $id = input('get.id/d');
         $field = input('get.field');

            if(!(is_numeric($id) && $id>0 && $field)){
            $this->error('非法参数');
        }

        $info = {$model}::field($field)->where('id',$id)->find();

        $this->success('ok','',$info[$field]);
    }
}